import { Component} from '@angular/core';
import { Storage } from '@ionic/storage';
import { NavController,ToastController} from 'ionic-angular';

import { CreateTask } from '../createtask/createtask';
import { TaskDetail } from '../taskdetail/taskdetail';
import { ApiService } from '../../providers/ApiService';

@Component({
  selector: 'page-filter',
  templateUrl: 'filter.html'
})
export class FilterPage {
  alltype = [
    {name:'全部'}
  ];
  infinite = null;
  receiveuser = [
    {id: "0", name: "全部", telephone: "", position: ""}
  ];
  projecttype = [
    {id: "0", project: "全部"}
  ];
  statuslist = [
    {display: "全部"}
  ];
  page = 1
  list = [];
  username;
  noMoreArticleTip = false;
  select_form = {
    date: '',
    display: '',
    type: '',
    name: '',
    tasktype: '',
    token:'',
  }
  constructor(
    public navCtrl: NavController,
    public storage: Storage,
    private apiService: ApiService,
    public toastCtrl: ToastController
  ){
    this.select_form = {
      date: 'all',
      display: '全部',
      type: '全部',
      name: '全部',
      tasktype: '全部',
      token:'',
    }
    this.storage.get('mytoken').then((mytoken)=>{
      if(mytoken) {
        this.select_form.token = mytoken;
      }
      this.getUser();
      this.getTaskType();
      this.getProjectType();
      this.getTaskStatus();
    })
  }
  ionViewDidEnter(){
    this.storage.get('select_form').then((select_form)=>{
      if(select_form) {
        this.select_form = select_form;
      }
    })
    this.storage.get('username').then((username)=>{
      if(username) {
        this.username = username;
      }
    })
  }
  //是否筛选一周内任务
  checktime(date){
    this.select_form.date = date;
  }
  //筛选任务内容
  filterItem(){
    this.list = [];
    this.storage.set('select_form', this.select_form)
    let body = {
      date: this.select_form.date,
      display: this.select_form.display,
      type: this.select_form.type,
      name: this.select_form.name,
      tasktype: this.select_form.tasktype,
      token:this.select_form.token,
      page:1
    }
    if(this.select_form.date == 'all'){
      body.date = ''
    }
    if(this.select_form.display == '全部'){
      body.display = ''
    }
    if(this.select_form.type == '全部'){
      body.type = ''
    }
    if(this.select_form.tasktype == '全部'){
      body.tasktype = ''
    }
    if(this.select_form.name == '全部'){
      body.name = ''
    }
    this.getlist(body);
  }
  getlist(body,load = null, callback = null){
    this.apiService.filterItems(body,response => {
      if(response.json().status== 1){
        let lists = response.json().content
        if (lists != null && lists.length > 0 && lists) {
          if (this.page == 1) {
            this.list = lists
          } else {
            this.list = this.list.concat(lists)
          }
          load && load.complete()
        }else{
          load && load.enable(false)
          this.noMoreArticleTip = true
        }
      }else{
        load && load.enable(false)
        this.noMoreArticleTip = true
        this.formTip(response.json().info)
      }
    })
  }
  //修改任务
  modifyTask(status,createuser,task_id){
    if(status == '待接手' && createuser == this.username){
      this.navCtrl.push(CreateTask,{
        taskid:task_id
      })
    }else{
      this.formTip('您没有修改权限');
    }
  }
  //获取任务状态
  getTaskStatus(){
    this.apiService.getStatus({token:this.select_form.token},response => {
      if(response.json().status== 1){
        this.statuslist = this.statuslist.concat(response.json().content);
      }
    })
  }
  //获取任务类型
  getTaskType(){
    this.apiService.getTaskTypes({token:this.select_form.token},response => {
      if(response.json().status== 1){
        this.alltype = this.alltype.concat(response.json().content);
      }
    })
  }
  //获取项目类型
  getProjectType(){
    this.apiService.getProjectTypes({token:this.select_form.token},response => {
      if(response.json().status== 1){
        this.projecttype = this.projecttype.concat(response.json().content);
      }
    })
  }
  //获取用户列表
  getUser(){
    this.apiService.getUsers({token:this.select_form.token},response => {
      if(response.json().status== 1){
        this.receiveuser = this.receiveuser.concat(response.json().content);
      }
    })
  }
  dismissHandler() {
    //console.info('Toast onDidDismiss()');
  }
  //消息提示函数
  formTip(hint){
    const toast = this.toastCtrl.create({
      message: hint,
      duration: 3000,
      position: 'middle'
    });
    toast.onDidDismiss(this.dismissHandler);
    toast.present();
    return;
  }
  goTaskDetail(id,state){
    this.navCtrl.push(TaskDetail,{
      id:id,
      state: state
    })
  }
  //下拉刷新
  doRefresh(ionRefresh){
    console.log('下拉刷新')
    this.page = 1
    let body = {
      date: this.select_form.date,
      display: this.select_form.display,
      type: this.select_form.type,
      name: this.select_form.name,
      tasktype: this.select_form.tasktype,
      token:this.select_form.token,
      page:this.page
    }
    this.getlist(body,ionRefresh);
  }
  //上拉加载
  doInfinite(infiniteScroll){
    console.log('上拉加载')
    this.page++
    if (this.infinite == null) {
      this.infinite = infiniteScroll
    }
    let body = {
      date: this.select_form.date,
      display: this.select_form.display,
      type: this.select_form.type,
      name: this.select_form.name,
      tasktype: this.select_form.tasktype,
      token:this.select_form.token,
      page:this.page
    }
    if(this.select_form.date == 'all'){
      body.date = ''
    }
    if(this.select_form.display == '全部'){
      body.display = ''
    }
    if(this.select_form.type == '全部'){
      body.type = ''
    }
    if(this.select_form.tasktype == '全部'){
      body.tasktype = ''
    }
    if(this.select_form.name == '全部'){
      body.name = ''
    }
    this.getlist(body,infiniteScroll);
  }
}
